package org.example.integration.adapter.product;

import org.example.cache.core.CacheScope;
import org.example.cache.stereotype.CachingMethod;
import org.example.cache.stereotype.MultiLayerCaching;
import org.example.order.domain.Product;
import org.example.order.domain.ProductRepository;
import org.example.product.api.ProductService;
import org.example.product.api.SdkProductDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@MultiLayerCaching(cacheName = "PRODUCT", scope = CacheScope.ALL)
public class ProductAdapter implements ProductRepository {
    private static final Logger logger = LoggerFactory.getLogger(ProductAdapter.class);
    private ProductService productService;

    public ProductAdapter(@Autowired ProductService productService) {
        this.productService = productService;
    }

    @CachingMethod()
    @Override
    public Product getProduct(String productId) {
        SdkProductDTO sdkProductDTO = productService.getProduct(productId);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Product, key: {}", productId);
        }
        return ProductConverter.INSTANCE.convert(sdkProductDTO);
    }

    @CachingMethod()
    @Override
    public List<Product> getProducts(List<String> productIds) {
        List<SdkProductDTO> sdkProductDTOList = productService.getProducts(productIds);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Products, keys: {}", String.join(",", productIds));
        }
        return ProductConverter.INSTANCE.convert(sdkProductDTOList);
    }
}
